package com.techysky.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.techysky.entity.MaintPlan;
import com.techysky.repository.MaintPlanRepository;
import com.techysky.service.MaintPlanService;
import com.techysky.util.CoreUtils;

/**
 * 
 * @author nichao
 * @date 2017年7月14日 上午9:34:07
 * @remark 业务实现类
 */
@Service("maintPlanService")
public class MaintPlanServiceImpl implements MaintPlanService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	private MaintPlanRepository maintPlanRepository;

	@Override
	public Map<String, Object> query(MaintPlan maintPlan) {
		Map<String,Object> result=new HashMap<>();
		maintPlan.setPage(maintPlan.getPage()!=null?maintPlan.getPage():0);
		maintPlan.setSize(maintPlan.getSize()!=null?maintPlan.getSize():8);
		List<MaintPlan> totalrows = this.maintPlanRepository.findAllLimit();
		Page<MaintPlan> resultList=this.maintPlanRepository.findAll(queryConditions(maintPlan),new PageRequest(maintPlan.getPage(),maintPlan.getSize()));
		List<MaintPlan> resultList2=resultList.getContent();
		/*for (MaintPlan maintPlan2 : resultList2) {
			if(null!=maintPlan2.getDataModule())
			maintPlan2.setRefsName(maintPlan2.getDataModule().getTitle());
		}*/
		result.put("page", maintPlan.getPage()+1);
		result.put("size", maintPlan.getSize());
		result.put("rows", CoreUtils.getInstance().convert(resultList2));
		result.put("total", resultList.getTotalElements());
		result.put("totalrows", totalrows);
		return result;
	}
	
	/**
	 * 等值匹配条件
	 * @return
	 */
	public Specification<MaintPlan> queryConditions(final MaintPlan maintPlan){
		return (Root<MaintPlan> root,CriteriaQuery<?> query, CriteriaBuilder cb)->{
			List<Predicate> predicate = new ArrayList<>();
			if(maintPlan!=null) {
				if(maintPlan.getProjectId()!=null) {
					predicate.add(cb.equal(root.get("projectId"), maintPlan.getProjectId()));		
				}
				if(maintPlan.getLimitCond()!=null&&maintPlan.getLimitCond()!=""){
            		if(!maintPlan.getLimitCond().contains(",")){
          				predicate.add(cb.equal(root.get("limitCond"), maintPlan.getLimitCond()));	
            		}else{
            			String[] str= maintPlan.getLimitCond().split(",");
            			predicate.add(root.get("limitCond").in(str));
            		}					
				}
			}
			Predicate[] pre = new Predicate[predicate.size()];
			query.orderBy(cb.asc(root.get("orderCode")));
			return query.where(predicate.toArray(pre)).getRestriction();
		};
	}

	@Override
	public MaintPlan saveOrUpdate(MaintPlan mp) {
		return this.maintPlanRepository.saveAndFlush(mp);
	}
	@Override
	public void saveTime(MaintPlan mp) {
		this.maintPlanRepository.updateTime(mp.getId(), mp.getCreateDate());
	}
}

